package com.jisuan.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisuan.contract.common.utils.PageResult;
import com.jisuan.contract.common.utils.Query;
import com.jisuan.contract.dao.BorrowingRecordDao;
import com.jisuan.contract.dao.BorrowingUserDao;
import com.jisuan.contract.entity.BorrowingRecordEntity;
import com.jisuan.contract.entity.BorrowingUserEntity;
import com.jisuan.contract.entity.SchoolContractEntity;
import com.jisuan.contract.service.BorrowingRecordService;
import com.jisuan.contract.service.SchoolContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("borrowingRecordService")
public class BorrowingRecordServiceImpl extends ServiceImpl<BorrowingRecordDao, BorrowingRecordEntity> implements BorrowingRecordService
{
  @Autowired
  SchoolContractService schoolContractService;

  @Autowired
  BorrowingUserDao borrowingUserDao;

    @Override
    public PageResult queryPage(Map<String, Object> params, BorrowingRecordEntity borrowingRecord)
    {
      LambdaQueryWrapper<BorrowingRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.like(
              borrowingRecord.getContractId() != null,
              BorrowingRecordEntity::getContractId, borrowingRecord.getContractId()
      );
      IPage<BorrowingRecordEntity> page = this.page(
              new Query<BorrowingRecordEntity>().getPage(params),
              queryWrapper
      );
      // 封装带 甲方乙方名称 的合同列表数据
      page.getRecords().forEach(entity -> {
        entity.setBorrowingName(
                borrowingUserDao.selectById(entity.getBorrowingPersonId()).getBorrowingName()
        );

        if (entity.getReturnPersonId() != null) {
          BorrowingUserEntity returnUserEntity = borrowingUserDao.selectById(entity.getReturnPersonId());
          entity.setReturnName(returnUserEntity.getBorrowingName());
        }
      });
      return new PageResult(page);
    }

  @Override
  public void borrow(BorrowingRecordEntity borrowingRecord)
  {
    baseMapper.insert(borrowingRecord);

    // 修改合同的借阅状态为 借阅
    SchoolContractEntity schoolContractEntity = new SchoolContractEntity();
    schoolContractEntity.setContractType(2);
    // schoolContractEntity.setId(borrowingRecord.getContractId());
    // schoolContractService.updateById(schoolContractEntity);
    schoolContractService.lambdaUpdate()
            .eq(SchoolContractEntity::getId, borrowingRecord.getContractId())
            .update(schoolContractEntity);
  }

  @Override
  public void cReturn(BorrowingRecordEntity borrowingRecord)
  {
    borrowingRecord.setReturnTime(new Date());
    baseMapper.updateById(borrowingRecord);

    // 修改合同的借阅状态为 在库
    SchoolContractEntity schoolContractEntity = new SchoolContractEntity();
    schoolContractEntity.setContractType(1);
    schoolContractService.lambdaUpdate()
            .eq(SchoolContractEntity::getId, borrowingRecord.getContractId())
            .update(schoolContractEntity);
  }

  @Override
  public BorrowingRecordEntity getByContractId(Long id)
  {
    List<BorrowingRecordEntity> list = lambdaQuery()
            .eq(BorrowingRecordEntity::getContractId, id)
            .isNull(BorrowingRecordEntity::getReturnPersonId)
            .list();
    if (list.isEmpty()) {
      return null;
    }
    BorrowingRecordEntity borrowingRecord = list.get(0);
    borrowingRecord.setBorrowingName(
            borrowingUserDao.selectById(borrowingRecord.getBorrowingPersonId()).getBorrowingName()
    );
    return borrowingRecord;
  }
}
